package com.adjy.pathbuilders.screens;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.TextImageButton;
import com.adjy.pathbuilders.objects.button.listeners.BackToMainMenuButtonListener;
import com.adjy.pathbuilders.objects.button.listeners.LevelSetterButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.state.Game;
import com.adjy.pathbuilders.world.Level;

public class LevelsMenu implements GameScreen {

	private List<ScreenPage> pages;
	private PageManager manager;
	
	public LevelsMenu(AnimationFactory factory, List<Level> levels, final Game game) {
		int buttonX = 200;
		int buttonY = 50;
		
		List<List<Button>> pagesButtons = getPagesButtons(factory, levels,
				game, buttonX, buttonY);
		
		Button exitButton = new TextImageButton(200, 400,
				new BackToMainMenuButtonListener(game),
				new WeakReferenceBitmap(factory, "menu_button_selected"),
				new WeakReferenceBitmap(factory, "menu_button_unselected"),
				game.getContext().getString(R.string.back));
		
		pages = createPages(factory, game, exitButton, pagesButtons);
		manager = new PageManager(pages);
	}

	private List<List<Button>> getPagesButtons(AnimationFactory factory,
			List<Level> levels, final Game game, int buttonX, int buttonY) {
		List<List<Button>> pagesButtons = new ArrayList<List<Button>>();
		pagesButtons.add(new ArrayList<Button>());
		int pageToAdd = 0;
		
		for (Level level : levels) {
			if (pagesButtons.get(pageToAdd).size() >= 3) {
				pageToAdd++;
				buttonY = 50;
				pagesButtons.add(new ArrayList<Button>());
			}
			Button button = new TextImageButton(buttonX, buttonY,
					new LevelSetterButtonListener(game, level),
					new WeakReferenceBitmap(factory, "menu_button_selected"),
					new WeakReferenceBitmap(factory, "menu_button_unselected"),
					level.getLevelID());
			buttonY += 80 + 50;
			pagesButtons.get(pageToAdd).add(button);
		}
		return pagesButtons;
	}

	private List<ScreenPage> createPages(AnimationFactory factory, final Game game,
			Button exitButton, List<List<Button>> pagesButtons) {
		List<ScreenPage> pages = new ArrayList<ScreenPage>();
		for (List<Button> pageButtons : pagesButtons) {
			pages.add(new ScreenPage(
					new WeakReferenceBitmap(factory, "main_menu_bg"), factory, exitButton, pageButtons));
		}
		
		return pages;
	}

	@Override
	public void load() {
		// TODO Auto-generated method stub

	}

	@Override
	public void release() {
		// TODO Auto-generated method stub

	}

	@Override
	public void input(MotionEvent event) {
		manager.manage(event);
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		manager.getCurrentPage().render(mcanvas, timeElapsed);
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub

	}
	
	public void reset() {
		manager.reset();
	}	
}
